/**
 * 项目删除接口测试用例
 *
 * Created by snowingsea on 2020/02/09.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');

module.exports = describe('/DELETE projects/:id.json', function () {
  function url(projectId) {
    return `/projects/${projectId}.json`;
  }
  const administrator = new Administrator();
  const newUserIds = [];
  let tester;

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((res) => {
        tester = res;
        newUserIds.push(tester.userId);
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'delete');
  common.should.be.tester(uri, url, 'delete');

  // 正常删除
  it('it should DELETE a project', function (done) {
    let newProject;
    tester.createRandomProject()
      .then((res) => {
        newProject = res;
        return tester.login();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(newProject.projectId))
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);
            // 查询
            chai.request(uri)
              .get(url(newProject.projectId))
              .set('Authorization', `Bearer ${tester.token.accessToken}`)
              .end((err, res) => {
                res.should.have.status(404);
                common.should.be.errorMessage(res.body);
                done();
              });
          });
      })
      .catch(done);
  });

  // 删除不存在的项目
  it('it should fail to delete a project when the project is not exists', function (done) {
    let projectId;
    tester.createRandomProject()
      .then((project) => {
        projectId = project.projectId;
        return tester.removeProject(projectId);
      })
      .then(() => tester.login())
      .then(() => {
        chai.request(uri)
          .delete(url(projectId))
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可删除他人的项目
  it('it should fail to delete a project when the project is not yours', function (done) {
    let otherProjectId;
    administrator.createRandomTester()
      .then((newTester) => {
        newUserIds.push(newTester.userId);
        return newTester.createRandomProject();
      })
      .then(({ projectId }) => {
        otherProjectId = projectId;
        return tester.login();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(otherProjectId))
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
